package com.cytech.cloudclinic.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.cytech.cloudclinic.R;
import com.cytech.cloudclinic.adapter.PrescriptionAcupointAdapter;
import com.cytech.cloudclinic.adapter.PrescriptionAcupointSelectAdapter;
import com.cytech.cloudclinic.base.BaseBean;
import com.cytech.cloudclinic.base.BaseObserver;
import com.cytech.cloudclinic.bean.Acupoint;
import com.cytech.cloudclinic.bean.AddDrugTag;
import com.cytech.cloudclinic.bean.Drug;
import com.cytech.cloudclinic.utils.RequestWithViewUtils;
import com.yechaoa.yutils.YUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class AcuPointDialog extends DialogFragment {
    @BindView(R.id.dialog_acupoint_tv_drug_name)
    TextView drugName;
    @BindView(R.id.diglog_acupoint_parent)
    RecyclerView parentRv;
    @BindView(R.id.diglog_acupoint_child)
    RecyclerView childRv;
    @BindView(R.id.rv_already_select_acupoint)
    RecyclerView selectRv;

    private PrescriptionAcupointAdapter parentAdapter;
    private List<Acupoint> mParentList;
    private PrescriptionAcupointAdapter childAdapter;
    private List<Acupoint> mChildList;
    private PrescriptionAcupointSelectAdapter selectAdapter;
    private List<Acupoint> mSelectList;

    private OnSaveAcuPointListener onSaveAcuPointListener;

    private int parentDrugTagId;
    private Drug drug;

    public AcuPointDialog(int parentDrugTagId, Drug drug,OnSaveAcuPointListener onSaveAcuPointListener) {
        this.onSaveAcuPointListener = onSaveAcuPointListener;
        Bundle bundle = new Bundle();
        bundle.putInt("parentDrugTagId",parentDrugTagId);
        bundle.putSerializable("drug",drug);
        setArguments(bundle);
    }

    @Override
    public void onStart() {
        super.onStart();
        WindowManager.LayoutParams lp = getDialog().getWindow().getAttributes();
        lp.width = (int) (0.9 * YUtils.getScreenWidth());
        lp.height = (int) (0.8 * YUtils.getScreenHeight());
        getDialog().getWindow().setAttributes(lp);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog_select_acu_point, null);
        ButterKnife.bind(this, view);
        builder.setView(view);

        parentDrugTagId = getArguments().getInt("parentDrugTagId");
        drug = (Drug) getArguments().getSerializable("drug");
        drugName.setText(drug.drug_name);

        initParentRv();
        initChildRv();
        initSelectRv();
        getAcupointParent(0);
        return builder.create();
    }

    private void initChildRv(){
        mChildList = new ArrayList<>();
        childAdapter = new PrescriptionAcupointAdapter(mChildList);
        childRv.setLayoutManager(new GridLayoutManager(getContext(),8));
        childRv.setAdapter(childAdapter);
        childAdapter.setCurrentPos(-1);
        childAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                selectAdapter.addData(childAdapter.getItem(position));
            }
        });

    }

    private void initParentRv(){
        mParentList = new ArrayList<>();
        parentAdapter = new PrescriptionAcupointAdapter(mParentList);
        parentRv.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL,false));
        parentRv.setAdapter(parentAdapter);
        parentAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                parentAdapter.setCurrentPos(position);
                getAcupointParent(parentAdapter.getItem(position).acupoint_id);
            }
        });

    }

    private void initSelectRv(){
        mSelectList = new ArrayList<>();
        selectAdapter = new PrescriptionAcupointSelectAdapter(mSelectList);
        selectRv.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL,false));
        selectRv.setAdapter(selectAdapter);
        selectAdapter.addChildClickViewIds(R.id.item_tag_delete);
        selectAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                selectAdapter.remove(position);
            }
        });
    }

    @OnClick(R.id.dialog_acupoint_cancel)
    public void onClickCancel(View view) {
        dismiss();
    }

    @OnClick(R.id.dialog_acupoint_save)
    public void onClickToll(View view) {
        if(onSaveAcuPointListener!=null){
            String pointStr = "";
            List<Acupoint> mSelectList = selectAdapter.getData();
            for(Acupoint point : mSelectList){
                pointStr += point.acupoint_name+"、";
            }
            if(!TextUtils.isEmpty(pointStr)){
                pointStr = pointStr.substring(0,pointStr.length()-1);
            }
            onSaveAcuPointListener.onSave(pointStr);
        }
        dismiss();
    }

    public void getAcupointParent(int parentId) {
        JSONObject json = new JSONObject();
        try {
            json.put("is_all",false);
            json.put("parent_id",parentId);
            json.put("drug_tag_id",parentDrugTagId);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        String requestStr = json.toString();
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestStr);
        RequestWithViewUtils.getInstance().addDisposable(RequestWithViewUtils.getInstance().apiServer.getAcuPoint(body), new BaseObserver<BaseBean<List<Acupoint>>>(null, false) {
            @Override
            public void onSuccess(BaseBean<List<Acupoint>> o) {
                if(parentId==0){
                    if(o.result!=null&&o.result.size()>0){
                        parentAdapter.setNewInstance(o.result);
                        getAcupointParent(o.result.get(0).acupoint_id);
                    }
                }else{
                    childAdapter.setNewInstance(o.result);
                }

            }

            @Override
            public void onError(String msg) {

            }
        });
    }

    public interface OnSaveAcuPointListener{
        void onSave(String acupointStr);
    }
}
